package fti.uksw.ruang;

import java.util.ArrayList;
import java.util.List;


import fti.uksw.ruang.R;
import fti.uksw.ruang.model.CalendarList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListRoomActivity extends ListActivity {

	public static String SELECTED_ROOM = "ROOM";

	AdapterListRoom adapter;
	ArrayList<CalendarList> calList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		calList = getIntent().getParcelableArrayListExtra(
				CalViewAct.TAG_LIST_ROOM);
		adapter = new AdapterListRoom(this, calList);

		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent returnIntent = new Intent(this, CalViewAct.class);
		CalendarList cl = adapter.getItem(position);
		returnIntent.putExtra(SELECTED_ROOM, cl);
		setResult(RESULT_OK, returnIntent);
		finish();
	}

	public class AdapterListRoom extends BaseAdapter {
		private Activity activity;
		private LayoutInflater inflater;
		private List<CalendarList> list;

		public AdapterListRoom(Activity a, List<CalendarList> calList) {
			activity = a;
			inflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			list = calList;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public CalendarList getItem(int pos) {
			return list.get(pos);
		}

		@Override
		public long getItemId(int position) {
			return list.get(position).getId();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.room_listview, null);
			}
			CalendarList _cl = list.get(position);

			TextView tvRoomName = (TextView) convertView
					.findViewById(R.id.tv_newroomlv_roomname);
			tvRoomName.setText(_cl.getCalendarTitle());
			return convertView;
		}

	}

}
